package com.yunfangdata.service.impl;

import com.yunfangdata.domain.Applicant;
import com.yunfangdata.domain.RecruitPlanDetail;
import com.yunfangdata.domain.Staff;
import com.yunfangdata.mapper.ApplicantMapper;
import com.yunfangdata.mapper.RecruitPlanDetailMapper;
import com.yunfangdata.mapper.StaffMapper;
import com.yunfangdata.query.QueryObject;
import com.yunfangdata.service.IApplicantService;
import com.yunfangdata.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApplicantServiceImpl implements IApplicantService {


    @Autowired
    private ApplicantMapper mapper;

    @Autowired
    private RecruitPlanDetailMapper detaelMapper;

    @Autowired
    private StaffMapper staffMapper;

    public void save(Applicant entity) {

        //部门状态默认为 正常
        mapper.insert(entity);
    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void update(Applicant entity) {
        mapper.updateByPrimaryKey(entity);
    }

    public Applicant get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Applicant> list() {
        return mapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int total = mapper.queryForCount(qo);

        if (total == 0){
            return new PageResult();
        }
        List<Applicant> rows = mapper.queryForList(qo);
        return new PageResult(total,rows);
    }


    //录用
    public void admission(Long id) {

        Applicant applicant = mapper.selectByPrimaryKey(id);

        //改变状态
        applicant.setStates(1);
        mapper.updateByPrimaryKey(applicant);

        //改变已录取人数
        Long detailId = applicant.getRecruitDetail().getId();
        RecruitPlanDetail recruitPlanDetail = detaelMapper.selectByPrimaryKey(detailId);
        int number = recruitPlanDetail.getCompletedNumber();
        number=number+1;
        recruitPlanDetail.setCompletedNumber(number);
        detaelMapper.updateByPrimaryKey(recruitPlanDetail);

        //保存信息到staff中去
        Staff staff = new Staff();
        staff.setName(applicant.getName());
        staffMapper.insert(staff);

    }

    //干掉
    public void disAdmission(Long id) {
        Applicant applicant = mapper.selectByPrimaryKey(id);
        //改变状态
        applicant.setStates(2);
        mapper.updateByPrimaryKey(applicant);
    }
}
